<?php

/**
 * 
 */
class SpecialServerService extends Service{

    public static function indexHeader(){
        $header = array(
            'id'            => array('text' => '序号', 'width' => '80px'),
            'name'          => array('text' => '专服名称', 'width' => '160px'),
        	'cname'         => array('text' => '专服别名', 'width' => '160px'),
            'special_id'    => array('text' => '专服ID', 'width' => '200px'),
        	'status'        => array('text' => '状态', 'width' => '100px'),
        	'api_synchro_url'   => array('text' => '同步URL', 'width' => '250px'),
        	'create_time'   => array('text' => '创建时间', 'width' => '150px'),
            'action'        => array('text' => '操作', 'width' => '60px'),
        );

        return $header;
    }

    public static function indexData($params){
        $modSpecialServer = new ModSpecialServer();
        $result = $modSpecialServer->page($params);

        $length = count($result['data']);
        $statusList = self::statusList();
        foreach($result['data'] as $k=>&$v){
        	$type = 1;
        	if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
        		$type = 2;
        	}
        	$v['status'] = $statusList[$v['status']];
            $v['create_time']     = date('Y-m-d H:i:s', $v['create_time']);
            $v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=specialServer/edit').'&id='.$v['id'].'">编辑</a>';
            $v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=specialServer/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';    
        }
        unset($v);

        return $result;
    }

    public static function doAdd($params){
        $user = AccAuth::me();

        $modSpecialServer = new ModSpecialServer();
        $modSpecialServer->game    = $params['g'];
        $modSpecialServer->name    = $params['name'];
        $modSpecialServer->special_id    = $params['special_id'];
        $modSpecialServer->create_user   = $user['UserID'];
        $modSpecialServer->create_time   = time();
        $modSpecialServer->update_user   = $user['UserID'];
        $modSpecialServer->update_time   = time();
        $modSpecialServer->cname = $params['cname'];
        $modSpecialServer->status = $params['status'];
        $modSpecialServer->api_synchro_url = $params['api_synchro_url'];

        return $modSpecialServer->save();
    }

    public static function doEdit($id, $params){
        if(empty($id)) return false;

        $user = AccAuth::me();

        $modSpecialServer = new ModSpecialServer();
        $update = array();
        $update['game']         = $params['g'];
        $update['name']         = $params['name'];
        $update['special_id']   = $params['special_id'];
        $update['update_user']  = $user['UserID'];
        $update['update_time']  = time();
        $update['cname']         = $params['cname'];
        $update['status']        = $params['status'];
        $update['api_synchro_url'] = $params['api_synchro_url'];

        $modSpecialServer->updateByPk($id, $update);
        return true;
    }
    
    

    public static function get($id){
    	$modSpecialServer = new ModSpecialServer();
        $obj = $modSpecialServer->findByPk($id);

        if($obj){
            $info = $obj->getAttributes();

            return $info;
        }else{
            return false;
        }
    }

    public static function delete($id){
        if(empty($id)) return false;

        $modSpecialServer = new ModSpecialServer();
        $modSpecialServer->deleteByPk($id);
        return true;
    }
    
    /*
     * 去重
     */
    public static function repeat($id = 0, $field, $val,$g){
    	$modSpecialServer = new ModSpecialServer();
    	$conut = $modSpecialServer->repeat($id, $field, $val,$g);
    	return $conut;
    }
    
    /**
     * 获取专服列表
     * @return multitype:unknown
     */
    public static function specialList(){
    	$game = PubBase::getCurrentGame();
    	$params = array();
    	$params['g'] = $game;
    	$modSpecialServer = new ModSpecialServer();
    	$data = $modSpecialServer->select($params);
    	$list = array();
    	foreach ($data as $key=>$val){
    		$list[$val['id']] = $val['name'];
    	}
    	return $list;
    		
    }
    
    /**
     * 根据专服id获取渠道
     * @param unknown $special_id 专服id
     */
    public static function getChannel($special_id){
    	if(!$special_id){
    		$result = array('status' => 0,'msg' => '参数错误','data'=> '');
    		return $result;
    	}
    	$modSpecialChannel = new ModSpecialChannel();
    	$data = $modSpecialChannel->channelForSpecialId($special_id);
    	$result = array('status' => 1,'msg' => '查询成功','data'=> $data);
    	return $result;
    }
    
    /**
     * 根据专服id获取服务器
     * @param unknown $special_id 专服id
     */
    public static function getServer($special_id){
    	if(!$special_id){
    		$result = array('status' => 0,'msg' => '参数错误','data'=> '');
    		return $result;
    	}
    	$modSpecialChannel = new ModSpecialChannel();
    	$data = $modSpecialChannel->serverForSpecialId($special_id);
    	$result = array('status' => 1,'msg' => '查询成功','data'=> $data);
    	return $result;
    }
    
    public static function statusList(){
    	return array(
    			'1'     => '正式运营',
    			'2'     => '下架',
    			'3'     => '测试平台',
    			 
    	);
    }
    
    /*
     * 获取所有
     * 专服-渠道关系数据
     * 专服=服务器关系数据
     * 存在服务器的专服才显示
     */
    public static function getAllServerChannel($game){
    	$modSpecialChannel = new ModSpecialChannel();
    	$server = $modSpecialChannel->serverForSpecialIdAll($game);
    	$channel = $modSpecialChannel->channelForSpecialIdAll($game);
    	
    	$channelList = array();
    	foreach ($channel as $key=>$val){
    		$channelList[$val['special_id']]['special_name'] = $val['special_name'];
    		$channelList[$val['special_id']]['channel'][$key]['channel_id'] = $val['channel_id'];
    		$channelList[$val['special_id']]['channel'][$key]['channel_name'] = $val['channel_name'];
    	}
    	
    	$serverList = array();
    	foreach ($server as $key=>$val){
    		$serverList[$val['special_id']]['special_id'] = $val['special_id'];
    		$serverList[$val['special_id']]['special_name'] = $val['special_name'];
    		$serverList[$val['special_id']]['server'][$key]['server_id'] = $val['server_id'];
    		$serverList[$val['special_id']]['server'][$key]['server_name'] = $val['server_name'];
    		$serverList[$val['special_id']]['server'][$key]['server_num'] = $val['server_num'];
    		$serverList[$val['special_id']]['channel'] = $channelList[$val['special_id']]['channel'];
    	}
    	
    	return $serverList;
    	
//     	echo '<pre/>';
//     	var_dump($serverList);exit;
    }


}